<?php

namespace addons\wwh\controller;

use think\Db;
use think\Request;

class Index extends Base
{
    
    /**
     * 首页
     */
    public function index()
    {
        $banner = Db::name('wwh_banner')->order('sort asc')->limit(6)->select();
        $this->assign('banner', $banner);
        
        $news = Db::name('wwh_archives')->where(['rec_data' => '1','status' => '1','classify'=>'news'])->order('time desc')->limit(4)->select();
        $this->assign('news', $news);

        $product = Db::name('wwh_archives')->where(['rec_data' => '1','status' => '1','classify'=>'product'])->order('id desc')->limit(6)->select();
        $this->assign('product', $product);
        
        $home = Db::name('wwh_home')->where('id', 1)->find();
        if ($home) {
            $this->assign('home', $home);
        } else {
            $this->error("请导入测试数据");
        }

        $diyname = $this->request->param('diyname/s');
        //获取当前活动分类
        $navCur = Db::name('wwh_column')->where('diyname', $diyname)->find();
        if (empty($navCur)) {
            $navCur['classify'] = 'none';
        }
        $this->view->assign('navCur', $navCur);
        return $this->view->fetch('/index');
    }

    /**
     * 栏目分类
     */
    public function column()
    {
        $diyname = $this->request->param('diyname/s');
        //获取当前活动分类
        $navCur = Db::name('wwh_column')->where('diyname', $diyname)->find();
        if (!$navCur) {
            $this->error('未知分类！');
        }
        $this->view->assign('navCur', $navCur);

        //获取当前栏目父分类ID
        do {
            $top = Db::name('wwh_column')->where(['id'=>$navCur['parent_id']])->find();
            if(!empty($navCur['parent_id']))
            {
                $navCur['parent_id'] = $top['parent_id'];
            }
        } while ($navCur['parent_id'] != 0);
        $this->view->assign('top', $top);

        if ($navCur['type'] === 'link') {
            $this->redirect($navCur['outlink']);
        }

        $map = [];
        $map['status'] = '1';
        if ($navCur['parent_id'] == 0) {
            $ids = Db::name('wwh_column')->where('parent_id', $navCur['id'])->column('id');
            $map['column_id'] = ['in', $ids];
        } else {
            $map['column_id'] = $navCur['id'];
        }
        //获取数据内容
        $keywords = input('keywords');
        $category = input('category');
        if (empty($keywords || $category)) {
            $data['list'] = Db::name('wwh_archives')->where(['status' => 1])->whereRaw('find_in_set('.$navCur['id'].', `column_ids`)')->paginate(6, false, ['query' => request()->param()]);
        } else {
            $map['title'] = ['like','%'.$keywords.'%'];
            $map['column_id'] = ['like','%'.$category.'%'];
            $map['status'] = '1';
            $data['list'] = Db::name('wwh_archives')->where($map)->order('weigh desc')->paginate(6, false, ['query' => request()->param()]);
        }
        $data['page'] = $data['list']->render();
        if (Request::instance()->isAjax()) {
            return json($data);
        }
        $this->assign('list', $data['list']);
        $this->assign('page', $data['page']);
        return $this->view->fetch($this->template .'/'.$navCur['tpl']);
    }

    /**
     * 内容数据
     */
    public function archives()
    {
        $diyname = $this->request->param('diyname/s');
        //获取当前活动分类
        $navCur = Db::name('wwh_column')->where('diyname', $diyname)->find();
        if (!$navCur) {
            $this->error('未知分类！');
        }
        $this->view->assign('navCur', $navCur);

        //获取当前栏目父分类ID
        do {
            $top = Db::name('wwh_column')->where(['id'=>$navCur['parent_id']])->find();
            $navCur['parent_id'] = $top['parent_id'];
        } while ($navCur['parent_id'] != 0);
        $this->view->assign('top', $top);

        $id = $this->request->param('id/d');
        $archives = Db::name('wwh_archives')
            ->alias('a')
            ->join('wwh_column c', 'c.id = a.column_id')
            ->field('a.*,c.parent_id,c.diyname,c.name')
            ->where('a.status', '1')
            ->where('a.id', $id)
            ->find();
        if (!$archives) {
            $this->error('内容未审核或不存在！');
        }

        $archives['lunbo'] = explode(',', $archives['images']);
        //上一篇下一篇
        $cfront=Db::name('wwh_archives')->where('id', '>', $id)->where(['classify' => 'cases', 'status' =>'1','column_id' => $archives['column_id']])->order('id asc')->limit('1')->find();    //方案上一篇
        $cafter=Db::name('wwh_archives')->where('id', '<', $id)->where(['classify' => 'cases', 'status' =>'1','column_id' => $archives['column_id']])->order('id desc')->limit('1')->find();   //方案下一篇
        $nfront=Db::name('wwh_archives')->where('id', '>', $id)->where(['classify' => 'news', 'status' =>'1','column_id' => $archives['column_id']])->order('id asc')->limit('1')->find();     //新闻上一篇
        $nafter=Db::name('wwh_archives')->where('id', '<', $id)->where(['classify' => 'news', 'status' =>'1','column_id' => $archives['column_id']])->order('id desc')->limit('1')->find();    //新闻下一篇
        Db::name('wwh_archives')->where('id', '=', $id)->setInc('views');   //自增浏览数

        $this->view->assign('cfront', $cfront);
        $this->view->assign('cafter', $cafter);
        $this->view->assign('nfront', $nfront);
        $this->view->assign('nafter', $nafter);
        $this->view->assign('archives', $archives);
        return $this->view->fetch($this->template.'/'.$archives['tpl']);
    }

    /**
     * 应聘简历
     */
    public function injoin()
    {
        // 获取ajax请求的值
        $verify = input("verify");
        // 进行验证码的验证
        if (!captcha_check($verify)) {
            $mes = 4;     //验证码不正确
        } else {
            $realname = input("realname");
            $tel = input("tel");
            $gangwei = input("gangwei");
            $content = input("content");
            $createtime = time();

            $file = request()->file('url');
            $rules = ['ext' => 'doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar','size' => 10000000];
            $info = $file->validate($rules)->move('../public/uploads/');
            if ($info) {
                // 成功上传后 获取上传信息
                $getSaveName=str_replace("\\", "/", $info->getSaveName());
                $url = '/uploads/'.$getSaveName;

                $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
                $url2 = $http_type . $_SERVER['HTTP_HOST'];

                $data =['realname'=>"$realname",'tel'=>"$tel",'gangwei'=>"$gangwei",'content'=>"$content",'url'=>"$url",'createtime'=>"$createtime"];
                $html = "<p><strong>姓名：</strong>$realname</p>
                     <p><strong>电话：</strong>$tel</p>
                     <p><strong>应聘岗位：</strong>$gangwei</p>
                     <p><strong>简历：</strong>$url2$url</p>";

                $config = get_addon_config("wwh");
                $hr = $config['hr'];

                $obj = \app\common\library\Email::instance();
                $result = $obj
                    ->to($hr)
                    ->subject('应聘简历')
                    ->message($html)
                    ->send();
                if ($result) {
                    $db = Db::name('wwh_injoin')->insert($data);
                    $this->assign('db', $db);
                    $mes = 1;        //提交成功
                }
                if (!$result) {
                    $mes = 2;        //请检查后台邮件配置|插件邮箱配置
                }
            } else {
                $mes = 3;            //上传文件后缀不允许
            }
        }
        return json($mes);
    }

    /**
     * 客户留言
     */
    public function message()
    {
        // 获取ajax请求的值
        $verify = input("verify");
        // 进行验证码的验证
        if (!captcha_check($verify)) {
            $this->error('验证码不正确');
        } else {
            $realname = input("realname");
            $company = input("company");
            $tel = input("tel");
            $email = input("email");
            $content = input("content");
            $createtime = time();
            $data =['realname'=>"$realname",'company'=>"$company",'tel'=>"$tel",'email'=>"$email",'content'=>"$content",'createtime'=>"$createtime"];

            $html = "<p><strong>公司名称：</strong>$company</p>
                     <p><strong>姓名：</strong>$realname</p>
                     <p><strong>邮箱：</strong>$email</p>
                     <p><strong>电话：</strong>$tel</p>
                     <p><strong>内容：</strong>$content</p>";

            $config = get_addon_config("wwh");
            $receive = $config['receive'];

            $obj = \app\common\library\Email::instance();
            $result = $obj
                ->to($receive)
                ->subject('客户留言')
                ->message($html)
                ->send();
            if ($result) {
                $db = Db::name('wwh_message')->insert($data);
                $this->assign('db', $db);
                $mes = 1;
            } elseif (!$result) {
                $mes = 2;
            } else {
                $mes = 3;
            }
        }
        return json($mes);
    }

    /**
     * 验证码
     */
    public function verify()
    {
        $captcha = new \think\captcha\Captcha();
        //验证码过期时间（s）
        $captcha->expire =1800;
        //验证码位数
        $captcha->length = 4;
        //验证成功后是否重置
        $captcha->reset = true;
        return $captcha->entry();
    }

    /**
     * 搜索页面
     */
    public function search()
    {
        $keywords = input('keywords');
        $this->assign('keywords', $keywords);
        if ($keywords) {
            $map['title'] = ['like','%'.$keywords.'%'];
            $map['status'] = '1';
            $map['classify'] = 'news';
            $data['list'] = Db::name('wwh_archives')->where($map)->paginate(6, false, ['query' => request()->param()]);
        } else {
            $data['list'] = Db::name('wwh_archives')->where(['status' => '1','classify' => 'news'])->paginate(6, false, ['query' => request()->param()]);
        }
        $data['page'] = $data['list']->render();
        if (Request::instance()->isAjax()) {
            return json($data);
        }
        $this->assign('list', $data['list']);
        $this->assign('page', $data['page']);

        $diyname = $this->request->param('diyname/s');
        //获取当前活动分类
        $navCur = Db::name('wwh_column')->where('diyname', $diyname)->find();
        if (empty($navCur)) {
            $navCur['classify'] = 'news';
        }
        $this->view->assign('navCur', $navCur);
        return $this->view->fetch('/search');
    }
}
